<template>
<div class="layerRtb layerRtb-threecolumn layerRtb-right">
    <three-title :title="{name:'日期选择'}" :close="false">
        <span tag="i" class="rig_close fr" @click="$parent.$parent.clickFiveShow()"></span>
    </three-title>
    <div class="layerRtb-scroll thinScroll" v-scrollHeight = "137">
        <div class="analyItem" v-for="(item, index) in dataName" :key="index">
            <p class="analyItemTit tx-center">{{item.name}}</p>
            <div class="analyItemCon">
                <el-date-picker
                    v-model="cloneValue[index]"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    @change="valueChange(index)"
                >
                </el-date-picker>
            </div>
        </div>
    </div>
    <div class="layerRtb-footer">
        <div class="analyItem">
            <p class="analyItemTit tx-center">综合</p>
            <div class="analyItemCon">
                <input type="button" class="uiBtn-normal uiBtn-blue" value="提交"  @click="submit()" v-if="buttonIsShow">
            </div>
        </div>
    </div>
</div>
</template>
<script>
import { findBeginDateOrderStandard, ChooseDate } from '../../../../Resources/Api/scene'
import { mapGetters, mapMutations } from 'vuex'
export default {
    props: ['type'],
    data () {
        return {
            dataName: [],
            dataValue: [],
            cloneValue: [],
            days: 0, // 时间差
            buttonIsShow: false // 是否显示按钮
        }
    },
    computed: {
        ...mapGetters(['userInfo', 'twoToThreeInfo', 'leftInfo'])
    },
    created () {
        this.findBeginDateOrderStandard()
        this.buttonType()
    },
    methods: {
        ...mapMutations({
            setUpDatePlate: `SET_UPDATE_PLATE`
        }),
        // 按钮显示与隐藏
        buttonType () {
            if (this.userInfo.as_diquId === 39) {
                if (this.twoToThreeInfo.status === this.type) {
                    this.buttonIsShow = true
                } else {
                    this.buttonIsShow = false
                }
            } else {
                const userLevel = this.userInfo.as_level
                if (userLevel.length > 0) {
                    userLevel.forEach(ele => {
                        if (Number(ele) === 47) {
                            if (this.twoToThreeInfo.status === this.type) {
                                this.buttonIsShow = true
                            } else {
                                this.buttonIsShow = false
                            }
                        }
                    })
                } else {
                    this.buttonIsShow = false
                }
            }
        },
        // input 改变
        valueChange (currentIndex) {
            this.cloneValue = []
            if (currentIndex === 0) {
                if (this.dataValue[0]) {
                    this.dataName.forEach((item, index) => {
                        if (index === 0) {
                            const beginDate = new Date(item.refTime)
                            const changeTime = new Date(this.dataValue[0])
                            // 求出两个时间的时间差，这个是天数
                            let num = 0
                            if (changeTime > beginDate) {
                                num = (changeTime - beginDate) / (1000 * 3600 * 24)
                            } else {
                                num = (beginDate - changeTime) / (1000 * 3600 * 24)
                            }
                            this.days = parseInt(Math.ceil(num)) - 1 // 转化为整天
                            this.cloneValue.push(this.dataValue[0])
                            this.$set(this.dataName[index], 'time', this.$utils.format('yyyy-MM-dd', this.dataValue[0]))
                            return false
                        } else {
                            this.valueChangeDate(item.refTime, index)
                        }
                    })
                }
            } else {
                this.cloneValue = this.dataValue
                this.$set(this.dataName[currentIndex], 'time', this.$utils.format('yyyy-MM-dd', this.dataValue[currentIndex]))
            }
        },
        // 获取改变后的日期
        valueChangeDate (time, index) {
            let data = new Date(time.replace(/-/g, '/')) // 把日期字符串转换成日期格式
            data = new Date((data / 1000 + (86400 * this.days)) * 1000) // 日期加1天
            this.cloneValue.push(this.$utils.format('yyyy-MM-dd', data))
            this.dataValue = this.cloneValue
            this.$set(this.dataName[index], 'time', this.$utils.format('yyyy-MM-dd', data))
        },
        // 获取开工日期
        findBeginDateOrderStandard () {
            let parm = {
                orderNo: this.leftInfo.orderno
            }
            // 根据条件阶段查询订单
            findBeginDateOrderStandard(parm).then((result) => {
                if (result.data.StatusCode === 1) {
                    this.dataName = result.data.Body.standardEntityList
                    this.dataName.forEach((item, index) => {
                        this.dataValue.push(item.time)
                    })
                }
                this.cloneValue = this.dataValue
            }).catch((error) => {
                console.log(error)
            })
        },
        // 提交日期
        submit () {
            if (this.twoToThreeInfo.timeoutFlag) {
                this.$message({
                    type: 'info',
                    message: '抱歉,订单超时,未提交成功'
                })
                return false
            }
            let submit = []
            this.dataName.forEach(item => {
                const currentData = {}
                currentData.id = item.id
                currentData.time = item.time
                submit.push(currentData)
            })
            let parm = {
                date: submit,
                creatUserNo: this.userInfo.as_cardNo,
                creatUserName: this.userInfo.as_fullName,
                orderNo: this.leftInfo.orderno
            }
            ChooseDate(parm).then((result) => {
                if (result.data.StatusCode === 1) {
                    this.setUpDatePlate()
                    this.$parent.$parent.clickFiveShow()
                    this.buttonIsShow = false
                }
            }).catch((error) => {
                console.log(error)
            })
        }
    }
}
</script>
